/* jQuery photoClip v1.5.1 | Relying on the plug-in [iscroll-zoom.js] [hammer.js] */
!function (t, o) {
    "use strict";
    "function" == typeof define && define.amd ? define(["jquery", "iscroll-zoom", "hammer"], o) : "object" == typeof exports ? module.exports = o(require("jquery"), require("iscroll-zoom"), require("hammer")) : o(t.jQuery, t.IScroll, t.Hammer)
}(this, function (t, o, e) {
    "use strict";
    function i(i, r) {
        function s() {
            O = !0, J.append(this), w.call(this, X, function () {
                Y = this.naturalWidth, Z = this.naturalHeight
            }), w(G, function () {
                l()
            }), A.call(this, this.src)
        }

        function c() {
            var t = {zoom: !0, scrollX: !0, scrollY: !0, freeScroll: !0, mouseWheel: !0, wheelAction: "zoom"};
            V = new o(B[0], t)
        }

        function l() {
            io = 0, no = 0, ao = 0, J.css({width: Y, height: Z}), S(J, io, no, ao), z(Y, Z), V.zoom(V.options.zoomStart), h(Y, Z);
            var t = .5 * (L - Y * V.options.zoomStart), o = .5 * (R - Z * V.options.zoomStart);
            V.scrollTo(t, o)
        }

        function h(t, o) {
            G.css({width: t, height: o}), B.append(G), V.refresh()
        }

        function p() {
            var t = !!navigator.userAgent.match(/mobile/i);
            if (t) {
                var o = new e(G[0]);
                o.add(new e.Rotate);
                var i, n;
                o.on("rotatemove", function (t) {
                    eo || (i = t.rotation, i > 180 ? i -= 360 : -180 > i && (i += 360), n = i > 0 ? 1 : 0 > i ? -1 : 0)
                }), o.on("rotateend", function (t) {
                    eo || Math.abs(i) > 30 && (1 == n ? d(t.center) : -1 == n && u(t.center))
                })
            } else G.on("dblclick", function (t) {
                d({x: t.clientX, y: t.clientY})
            })
        }

        function d(t) {
            f(90, t)
        }

        function u(t) {
            f(-90, t)
        }

        function f(t, o) {
            if (!eo) {
                eo = !0;
                var e;
                e = o ? x(G, o.x, o.y) : b(G, B, .5 * L, .5 * R);
                var i, n, a = y(ao, e), r = a.x, s = a.y, c = 0, l = 0, p = 0, d = 0, u = ao + t;
                90 == u || -270 == u ? (c = r + s, l = s - r, u > ao ? (p = Z - r - s, d = r - s) : ao > u && (p = Z - s - (Y - r), d = r + s - Z), i = Z, n = Y) : 180 == u || -180 == u ? (c = 2 * r, l = 2 * s, u > ao ? (p = Y - r - (Z - s), d = Z - (r + s)) : ao > u && (p = Y - (r + s), d = Z - s - (Y - r)), i = Y, n = Z) : 270 == u || -90 == u ? (c = r - s, l = r + s, u > ao ? (p = r + s - Y, d = Y - r - (Z - s)) : ao > u && (p = s - r, d = Y - r - s), i = Z, n = Y) : (0 == u || 360 == u || -360 == u) && (c = 0, l = 0, u > ao ? (p = r - s, d = r + s - Y) : ao > u && (p = r + s - Z, d = s - r), i = Y, n = Z), 0 == ao ? (io = 0, no = 0) : 90 == ao || -270 == ao ? (io -= r + s, no -= s - r) : 180 == ao || -180 == ao ? (io -= 2 * r, no -= 2 * s) : (270 == ao || -90 == ao) && (io -= r - s, no -= r + s), io = io.toFixed(2) - 0, no = no.toFixed(2) - 0, S(J, io, no, ao, r, s), F(J, io, no, u, 200, function () {
                    eo = !1, ao = u % 360, io += c + p, no += l + d, io = io.toFixed(2) - 0, no = no.toFixed(2) - 0, S(J, io, no, ao), V.scrollTo(V.x - p * V.scale, V.y - d * V.scale), z(i, n), V.scale < V.options.zoomMin && V.zoom(V.options.zoomMin), h(i, n)
                })
            }
        }

        function m() {
            N = document.createElement("canvas"), N.width = L, N.height = R
        }

        function g() {
            if (!O)return void alert("亲，当前没有图片可以裁剪!");
            var t = b(G, B), o = V.scale, e = N.getContext("2d");
            e.clearRect(0, 0, N.width, N.height), e.save(), H ? e.scale(o, o) : (N.width = L / o, N.height = R / o), e.translate(io - t.x / o, no - t.y / o), e.rotate(ao * Math.PI / 180), e.drawImage(X[0], 0, 0), e.restore();
            var i = N.toDataURL(I, 1);
            K.css("background-image", "url(" + i + ")"), D.call(X[0], i)
        }

        function v() {
            w(Q, function () {
                $ = Q.width(), _ = Q.height()
            })
        }

        function b(t, o, e, i) {
            e = e || 0, i = i || 0;
            var n, a;
            return w(t, function () {
                n = t.offset()
            }), w(o, function () {
                a = o.offset()
            }), {x: a.left - n.left + e, y: a.top - n.top + i}
        }

        function x(t, o, e) {
            o = o || 0, e = e || 0;
            var i;
            return w(t, function () {
                i = t.offset()
            }), {x: o + oo.scrollLeft() - i.left, y: e + oo.scrollTop() - i.top}
        }

        function w(o, e) {
            var i = t();
            t.each(o, function (o, e) {
                for (var n, a = t(e), r = a.parents().andSelf().filter(":hidden"), o = 0; o < r.length && a.is(":hidden"); o++)n = r.eq(o), "none" == n.css("display") && (i = i.add(n.show()))
            }), "function" == typeof e && e.call(this), i.hide()
        }

        function y(t, o) {
            var e = V.scale, i = {};
            return 0 == t ? (i.x = o.x / e, i.y = o.y / e) : 90 == t || -270 == t ? (i.x = o.y / e, i.y = Z - o.x / e) : 180 == t || -180 == t ? (i.x = Y - o.x / e, i.y = Z - o.y / e) : (270 == t || -90 == t) && (i.x = Y - o.y / e, i.y = o.x / e), i
        }

        function k(t, o, e, i) {
            var n = t / e, a = o / i;
            return n > a ? n : a
        }

        function z(t, o) {
            V.options.zoomMin = k(L, R, t, o), V.options.zoomMax = Math.max(1, V.options.zoomMin), V.options.zoomStart = Math.min(V.options.zoomMax, k($, _, t, o))
        }

        function T(t, o, e, i) {
            o = o || .8, e = e || 0;
            var n = i || "image/jpeg", a = t.naturalWidth, r = t.naturalHeight, s = Math.max(a, r);
            if (s > 1024) {
                var c = Math.min(a, r);
                c = c / s * 1024, s = 1024, a > r ? (a = s, r = c) : (a = c, r = s)
            }
            var l = document.createElement("canvas"), h = l.getContext("2d");
            e ? (l.width = r, l.height = a, h.translate(r, 0), h.rotate(e * Math.PI / 180)) : (l.width = a, l.height = r), h.drawImage(t, 0, 0, a, r);
            var p = l.toDataURL(n, o || .8);
            return p
        }

        function M(o) {
            X && X.length && (X.remove(), delete X[0]), X = t("<img>").css({"user-select": "none", "pointer-events": "none"}), X.load(s), X.attr("src", o)
        }

        function S(t, o, e, i, n, r) {
            n = n || 0, r = r || 0;
            var s = {};
            s[a + "transform"] = "translateZ(0) translate(" + o + "px," + e + "px) rotate(" + i + "deg)", s[a + "transform-origin"] = n + "px " + r + "px", t.css(s)
        }

        function F(t, o, e, i, r, s) {
            t.css(a + "transform"), t.css(a + "transition", a + "transform " + r + "ms"), t.one(n, function () {
                t.css(a + "transition", ""), s.call(this)
            }), t.css(a + "transform", "translateZ(0) translate(" + o + "px," + e + "px) rotate(" + i + "deg)")
        }

        function j() {
            Q = t(i).css({"user-select": "none", overflow: "hidden"}), "static" == Q.css("position") && Q.css("position", "relative"), B = t("<div class='photo-clip-view'>").css({position: "absolute", left: "50%", top: "50%", width: L, height: R, "margin-left": -L / 2, "margin-top": -R / 2}).appendTo(Q), G = t("<div class='photo-clip-moveLayer'>").appendTo(B), J = t("<div class='photo-clip-rotateLayer'>").appendTo(G);
            {
                var o = t("<div class='photo-clip-mask'>").css({position: "absolute", left: 0, top: 0, width: "100%", height: "100%", "pointer-events": "none"}).appendTo(Q);
                t("<div class='photo-clip-mask-left'>").css({position: "absolute", left: 0, right: "50%", top: "50%", bottom: "50%", width: "auto", height: R, "margin-right": L / 2, "margin-top": -R / 2, "margin-bottom": -R / 2, "background-color": "rgba(0,0,0,.5)"}).appendTo(o), t("<div class='photo-clip-mask-right'>").css({position: "absolute", left: "50%", right: 0, top: "50%", bottom: "50%", "margin-left": L / 2, "margin-top": -R / 2, "margin-bottom": -R / 2, "background-color": "rgba(0,0,0,.5)"}).appendTo(o), t("<div class='photo-clip-mask-top'>").css({position: "absolute", left: 0, right: 0, top: 0, bottom: "50%", "margin-bottom": R / 2, "background-color": "rgba(0,0,0,.5)"}).appendTo(o), t("<div class='photo-clip-mask-bottom'>").css({position: "absolute", left: 0, right: 0, top: "50%", bottom: 0, "margin-top": R / 2, "background-color": "rgba(0,0,0,.5)"}).appendTo(o), t("<div class='photo-clip-area'>").css({border: "1px dashed #ddd", position: "absolute", left: "50%", top: "50%", width: L, height: R, "margin-left": -L / 2 - 1, "margin-top": -R / 2 - 1}).appendTo(o)
            }
            K = t(q), K.length && K.css({"background-color": "#666", "background-repeat": "no-repeat", "background-position": "center", "background-size": "contain"})
        }

        var L = r.width, R = r.height, C = r.file, q = r.view, E = r.ok, I = r.outputType || "image/jpeg", H = r.strictSize, W = r.loadStart, A = r.loadComplete, P = r.loadError, D = r.clipFinish;
        "jpg" === I ? I = "image/jpeg" : "png" === I && (I = "image/png");
        var U = t(C);
        if (U.length) {
            var X, Y, Z, O;
            U.attr("accept", "image/*"), U.change(function () {
                if (this.files.length) {
                    if (!/image\/\w+/.test(this.files[0].type))return alert("图片格式不正确，请选择正确格式的图片文件！"), !1;
                    var o = new FileReader;
                    o.onprogress = function (t) {
                        console.log((t.loaded / t.total * 100).toFixed() + "%")
                    }, o.onload = function (o) {
                        var e = o.total / 1024;
                        if (e > 1024) {
                            var i = 1024 / e, n = t("<img>").hide();
                            n.load(function () {
                                var t = this.naturalWidth;
                                n.appendTo(document.body);
                                var o = this.naturalHeight;
                                n.remove(), delete n[0], n = null;
                                var e = 0;
                                t == o && (e = 90);
                                var a = T(this, i, e, I);
                                M(a)
                            }), n.attr("src", this.result)
                        } else M(this.result)
                    }, o.onerror = function (t) {
                        alert("图片加载失败"), P.call(this, t)
                    }, o.readAsDataURL(this.files[0]), W.call(o, this.files[0])
                }
            }), U.click(function () {
                this.value = ""
            });
            var Q, B, G, J, K, N, V, $, _;
            j(), c(), p(), m();
            var to = t(E);
            to.length && to.click(function () {
                g()
            });
            var oo = t(window);
            v(), oo.resize(v);
            var eo, io, no, ao
        }
    }

    t.fn.photoClip = function (o) {
        if (!window.FileReader)return void alert("您的浏览器不支持 HTML5 的 FileReader API， 因此无法初始化图片裁剪插件，请更换最新的浏览器！");
        var e = {width: 200, height: 200, file: "", view: "", ok: "", outputType: "jpg", strictSize: !1, loadStart: function () {
        }, loadComplete: function () {
        }, loadError: function () {
        }, clipFinish: function () {
        }};
        return t.extend(e, o), this.each(function () {
            i(this, e)
        }), this
    };
    var n, a = "";
    return function () {
        var t, o = {Webkit: "webkit", Moz: "", O: "o"}, e = document.documentElement, i = function (o) {
            return t ? t + o : o.toLowerCase()
        };
        for (var r in o)if (void 0 !== e.style[r + "TransitionProperty"]) {
            a = "-" + r.toLowerCase() + "-", t = o[r];
            break
        }
        n = i("TransitionEnd")
    }(), t
});